package com.atguigu.lease.web.app.service.impl;

import com.atguigu.lease.common.context.LoginUserContext;
import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.model.enums.LeaseStatus;
import com.atguigu.lease.web.app.mapper.*;
import com.atguigu.lease.web.app.service.ApartmentInfoService;
import com.atguigu.lease.web.app.service.BrowsingHistoryService;
import com.atguigu.lease.web.app.service.RoomInfoService;
import com.atguigu.lease.web.app.vo.apartment.ApartmentItemVo;
import com.atguigu.lease.web.app.vo.attr.AttrValueVo;
import com.atguigu.lease.web.app.vo.fee.FeeValueVo;
import com.atguigu.lease.web.app.vo.graph.GraphVo;
import com.atguigu.lease.web.app.vo.room.RoomDetailVo;
import com.atguigu.lease.web.app.vo.room.RoomItemVo;
import com.atguigu.lease.web.app.vo.room.RoomQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author liubo
 * @description 针对表【room_info(房间信息表)】的数据库操作Service实现
 * @createDate 2023-07-26 11:12:39
 */
@Service
@Slf4j
public class RoomInfoServiceImpl extends ServiceImpl<RoomInfoMapper, RoomInfo>
        implements RoomInfoService {
    @Autowired
    RoomInfoMapper roomInfoMapper;
    @Autowired
    LeaseAgreementMapper leaseAgreementMapper;
    @Autowired
    ApartmentInfoService apartmentInfoService;
    @Autowired
    LeaseTermMapper leaseTermMapper;
    @Autowired
    PaymentTypeMapper paymentTypeMapper;
    @Autowired
    FeeValueMapper feeValueMapper;
    @Autowired
    AttrValueMapper attrValueMapper;
    @Autowired
    GraphInfoMapper graphInfoMapper;
    @Autowired
    LabelInfoMapper labelInfoMapper;
    @Autowired
    FacilityInfoMapper facilityInfoMapper;
    @Autowired
    private BrowsingHistoryService browsingHistoryService;

    @Override
    public IPage<RoomItemVo> pageItem(Page<RoomItemVo> roomItemVoPage, RoomQueryVo queryVo) {
        /*
        todo 1、查询房间的基本信息
        todo 2、查询房间对应的公寓信息
        todo 3、查询房间对应的租赁协议信息
        todo 4、查询房间图片信息
            查询图片的时候需要注意，一个房间可能有多张图片，但是我们只能给前端返回一个房间信息，和多张表图片的集合，以 《房间  图片集合》的形式展示，所以需要嵌套查询
        todo 5、查询房间对应的标签信息
         */
        return roomInfoMapper.pageItem(roomItemVoPage,queryVo);
    }

    @Override
    public RoomDetailVo getDetailById(Long id) {
        //实例化一个返回值对象
        RoomDetailVo roomDetailVo = new RoomDetailVo();
        //获取房间的基本信息
        RoomInfo roomInfo = this.getById(id);
        //将房间基本信息赋值给返回值对象
        BeanUtils.copyProperties(roomInfo,roomDetailVo);
        //获取房间对应的公寓信息
        ApartmentItemVo apartmentItemVo = apartmentInfoService.selectApartmentById(roomInfo.getApartmentId());
        //获取房间的图片
         List<GraphInfo> graphVoList= graphInfoMapper.selectGraphByItemIdAndItemType(id,ItemType.ROOM);
        //获取房间的属性信息
         List<AttrValueVo> attrValueVoList=attrValueMapper.selectAttrValueByRoomId(id);
         //获取房间的配套信息
         List<FacilityInfo> facilityInfoList= facilityInfoMapper.selectFacilityByRoomId(id);
         //获取房间的标签信息
         List<LabelInfo> labelInfoList= labelInfoMapper.selectLabelByRoomId(id);
         //获取房间的支付方式
         List<PaymentType> paymentTypeList=paymentTypeMapper.selectPaymentTypeByRoomId(id);
         //获取公寓的杂费信息
         List<FeeValueVo> feeValueVoList= feeValueMapper.selectByApartmentId(roomInfo.getApartmentId());
         //获取房间的租赁信息
         List<LeaseTerm> leaseTermList= leaseTermMapper.selectLeaseTermByRoomId(id);
         //确认房间时候被删除
         Boolean isDelete =roomInfo.getIsDeleted()==1;
         //确认房间时候被入住
        Long count = leaseAgreementMapper.selectCount(new LambdaQueryWrapper<LeaseAgreement>().eq(LeaseAgreement::getRoomId, id).in(LeaseAgreement::getStatus, 2, 5));
        Boolean isCheckIn = count > 0;
        roomDetailVo.setLeaseTermList(leaseTermList);
        roomDetailVo.setIsCheckIn(isCheckIn);
        roomDetailVo.setIsDelete(isDelete);
        roomDetailVo.setFeeValueVoList(feeValueVoList);
        roomDetailVo.setGraphVoList(graphVoList);
        roomDetailVo.setFacilityInfoList(facilityInfoList);
        roomDetailVo.setLabelInfoList(labelInfoList);
        roomDetailVo.setPaymentTypeList(paymentTypeList);
        roomDetailVo.setAttrValueVoList(attrValueVoList);
        roomDetailVo.setApartmentItemVo(apartmentItemVo);

        //添加浏览历史记录
        //todo 1、获取当前登录用户
browsingHistoryService.saveHistory(LoginUserContext.getLoginUser().getUserId(),id);//传入当前登录用户和房间id

        return roomDetailVo;
    }

    @Override
    public IPage<RoomItemVo> pageItemInfo(Page<RoomItemVo> roomItemVoPage, Long id) {
        return roomInfoMapper.pageItemInfo(roomItemVoPage,id);
    }
}




